<?
	if (!defined('i-Teck_ADMIN')) die("Hacking attempt");

	switch($mod){
		case "add":
			
		?>
<form name="form1" method="post" action="">
  <table width="600" align="center">
 
    <tr>
      <td height="22" colspan="2" class="fr_top">Thêm loại sản phẩm</td>
    </tr>
    <tr>
      <td width="99" height="22" class="fr">Tên loại sản phẩm</td>
      <td width="155" class="fr_2"><label>
        <input name="name" type="text" id="name" size="50">
      </label></td>
    </tr>
    <tr>
      <td height="21" class="fr">Nhãn hiệu</td>
      <td class="fr_2">
      <select name="id_trademarks" id="id_trademarks">
       <? $result=mysql_query("select * from shop_trademarks");
		 while($r=mysql_fetch_array($result)){ ?>
         
         <option value="<? echo $r["id"];?>"><? echo $r["name"];?></option>
    	<? }?>
        </select>
     </td>
    </tr>
    <tr>
      <td class="fr" height="25" colspan="2" align="center"><label>
        <input type="submit" name="cmd" id="cmd" value="Thêm">
      </label></td>
    </tr>
  </table>
</form> 
<?
if($_REQUEST["cmd"]=="Thêm"){
	mysql_query("insert into shop_category (name,id_trademarks) 
				values ('".addslashes($_POST["name"])."','".intval($_POST["id_trademarks"])."')");
	mysql_close();
	echo "<meta http-equiv='refresh' content='0; url=?act=category&mod=view'>";
	}
	break;
	case "view":
	$result=mysql_query("select * from shop_category");
	
	$sobanghi=15; //Số thể loại hiển thị trong 1 trang
	$sotrang = ceil(mysql_num_rows($result)/$sobanghi);
	$page = intval($_REQUEST["page"]);
	if ($page<=0) $page=1;
	if ($page>$sotrang)  $page=$sotrang;
	$result2=mysql_query("select * from shop_category order by id desc limit ".($page-1)*$sobanghi.",".$sobanghi);
	
	if(mysql_num_rows($result)<=0){echo"<center><font color=red>Chưa có dữ liệu</font></center>";}
	else{
	?>
<h3 align="center">Quản lý loại sản phẩm</h3>
	  <table width="600" border="0" align="center">
    <tr class="fr_top">
      <td width="24" height="23">ID</td>
      <td width="346">Loại sản phẩm</td>
      <td width="142"><strong>Nhãn hiệu</td>
      <td width="33">Sửa</td>
      <td width="33">Xóa</td>
    </tr>
	<?  while($r=mysql_fetch_array($result2)){?>
    <tr>
      <td height="23" class="fr"><? echo $r['id'];?></td>
      <td class="fr"><a href=?act=category&mod=edit&id=<? echo $r['id'];?>><? echo stripslashes($r['name']);?></a></td>
      <? $id=$r["id_trademarks"];
	$result1=mysql_query("select * from shop_trademarks where id=$id");
	while($r1=mysql_fetch_array($result1)){
	?>
    <td class="fr_2"><? echo stripslashes($r1["name"]); }?></td>
      <td class="fr_2"><a href=?act=category&mod=edit&id=<? echo $r['id'];?>>Sửa</a></td>
      <td class="fr_2"><a onClick="return  confirm('Bạn có muốn xóa thể loại này ko');" href=?act=category&mod=delete&id=<? echo $r['id'];?>>Xóa</a></td>
    </tr>
	<? }}?>
 <div align="center">
<? showPageNavigation($page,$sotrang, '','?act=category&mod=view&page=');?> 
</center>
    
  </table>
	
	
	<?  break;
		case "edit":
		$id=intval($_REQUEST["id"]);
		$result=mysql_query("select * from shop_category where id=$id");
		if(mysql_num_rows($result)<=0){echo"<center><font color=red>Chưa có dữ liệu</font</center>";}
		else{
		?>
        <form name="form1" method="post" action="">
          <table width="600" border="0" align="center">
 
    <tr>
      <td colspan="2" class="fr_top">Sửa loại sản phẩm</td>
    </tr>
    <tr>
      <td width="99" class="fr">Tên loại sản phẩm</td>
      <td width="155" class="fr_2">
      <? while($r=mysql_fetch_array($result)){ ?>
        <input type="text" name="name" value="<? echo stripslashes($r["name"]);?>">
        <? }?>
      </td>
    </tr>
    <tr>
      <td class="fr">Nhãn hiệu</td>
      <td class="fr_2">
      <select name="trademarks" id="trademarks">
      
      <? $result=mysql_query("select * from shop_trademarks");
	  $selected =  mysql_fetch_array(mysql_query("select * from shop_category where id=$id"));
	  while($r=mysql_fetch_array($result)){ ?>
       <option value="<? echo $r["id"];?>"<? if($r["id"]==$selected["id_trademarks"]){echo " selected='selected'";}?>><? echo stripslashes($r["name"]);?></option>
       <? } ?>
       
        </select>
     </td>
    </tr>
    <tr>
      <td height="28" colspan="2" align="center" class="fr"><label>
        <input type="submit" name="cmd" id="cmd" value="Sửa">
        </label></td>
    </tr>
  </table>
</form>
		
	<? } if($_REQUEST["cmd"]=="Sửa"){
		mysql_query("update shop_category set name='".addslashes($_POST["name"])."', id_trademarks='".intval($_POST["trademarks"])."' where id=$id");
		mysql_close();
		echo "<meta http-equiv='refresh' content='0; url=?act=category&mod=view'>"; }
	
	break;
	case "delete":
		$id=intval($_REQUEST["id"]);
		mysql_query("delete from shop_category where id=$id");
		mysql_close();
		echo "<meta http-equiv='refresh' content='0; url=?act=category&mod=view'>";
	break;
	
	} 
	?>
